*Importing data-file

use Data2022ref

sort common_id

******Cleaning data before reshaping to long format******
codebook Q1_pre Q1_o1_pre Q3_pre Q3_o1_pre Q11_pre Q22_pre Q22_o1_pre Q1_post Q1_o1_post Q3_post Q3_o1_post Q5_post_post Q9_post_post Q9B_post Q10_post_post Q22_post Q22_o1_post Q23_post_post Q24_post_post Q25_post_post Q40_post_post

drop Q1_pre Q3_pre Q22_pre Q37_1_resp_pre Q37_2_resp_pre Q37_3_resp_pre Køn_pre Alder_pre Kommune_pre Uddannelse_pre partivalg_sidste_fv_pre Q1_post Q3_post Q5B_1_resp_post Q5B_2_resp_post Q5B_3_resp_post Q5B_4_resp_post Q5B_5_resp_post Q5B_6_resp_post Q5C_1_resp_post Q5C_2_resp_post Q5C_3_resp_post Q5C_4_resp_post Q5C_5_resp_post Q5C_6_resp_post Q5D_Text_post Q5D_1_resp_post Q5D_2_resp_post Q5D_3_resp_post Q5D_4_resp_post Q5D_5_resp_post Q11_pre Q22_post Køn_post Alder_post Kommune_post Uddannelse_post partivalg_sidste_fv_post 

rename Q1_o1_pre Q1_pre
rename Q3_o1_pre Q3_pre
rename Q22_o1_pre Q22_pre
rename Q1_o1_post Q1_post
rename Q3_o1_post Q3_post
rename Q5_post_post Q5_post
rename Q9B_post whendecided
rename Q9_post_post Q9_post
rename Q10_post_post Q10_post
rename Q22_o1_post Q22_post
rename Q23_post_post Q23_post
rename Q24_post_post Q24_post
rename Q25_post_post Q25_post
rename Q40_post_post Q40_post

**********Reshaping data from wide to long format to prepare for analysis************
reshape long Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q12 Q13 Q14 Q15 Q16 Q17_1_resp Q17_2_resp Q19 Q20 Q21 Q22 Q23 Q24 Q25 Q26 Q27 Q28 Q29_1_resp Q29_2_resp Q29_3_resp Q29_4_resp Q33 Q34_1_resp Q34_2_resp Q36 Q40 Region weight1 weight2 weight3, i(common_id) j(time) string



*Creating a time dummy variable indicating whether respondents answered before or after the campaign
codebook time
encode time, gen(time1)
codebook time1
recode time1 (1=1 "Wave 2") (2=0 "Wave 1"), gen(timedummy)
codebook timedummy



*******Creating dependent variable (vote inclinations/policy opinions)********
codebook Q5
recode Q5 (1=1 "Yes (in favour of proposal)") (2 3 4 5=0 "No (not supporting proposal"), gen(vote_yes)
codebook Q5 vote_yes



**********Creating learning variable (independent variable)*********


*Perception of EU army question
codebook Q34_1_resp
recode Q34_1_resp (1 2 3 6=0 "Uninformed") (4 5=1 "Correctly Informed"), gen(eu_army_perception)
codebook eu_army_perception

gen learn_policyinfo=.
sort common_id timedummy
by common_id (timedummy), sort: replace learn_policyinfo=0 if eu_army_perception[_n-1]==0 & (eu_army_perception==0)
by common_id (timedummy), sort: replace learn_policyinfo=1 if eu_army_perception[_n-1]==0 & (eu_army_perception==1)
by common_id (timedummy), sort: replace learn_policyinfo=2 if eu_army_perception[_n-1]==1 & (eu_army_perception==1)
by common_id (timedummy), sort: replace learn_policyinfo=3 if eu_army_perception[_n-1]==1 & (eu_army_perception==0)
label define learn_policyinfo 0 "Never Learned" 1 "Learned Policy Info" 2 "Already Knew" 3 "Forgot"
label values learn_policyinfo learn_policyinfo
codebook learn_policyinfo

bro common_id timedummy learn_policyinfo eu_army_perception

*Adding values to misssing observation regarding independent variable (learning)
bysort common_id (timedummy) : replace learn_policyinfo = learn_policyinfo[_n+1] if missing(learn_policyinfo)

*Checking coding was made correct
bro common_id timedummy eu_army_perception learn_policyinfo
tab timedummy learn_policyinfo, row





************************Control Variables************************



*******Time-invariant background variables - used for matching and control for unispecific time-trends********

*Gender
codebook Q2
gen gender=Q2 if timedummy==0
bysort common_id (timedummy) : replace gender = gender[_n-1] if missing(gender)
codebook Q2 gender if timedummy==0
recode gender (1 3=1 "Male") (2=2 "Female"), gen(gender1)
drop gender
rename gender1 gender
codebook gender
tab timedummy gender, row

*Age
codebook Q1, tab(99)
gen age=Q1 if timedummy==0
bysort common_id (timedummy) : replace age = age[_n-1] if missing(age)
sum Q1 age if timedummy==0

gen age_cat=.
replace age_cat=1 if inrange(age, 17, 29.1)
replace age_cat=2 if inrange(age, 29.9, 39.1)
replace age_cat=3 if inrange(age, 39.9, 49.1)
replace age_cat=4 if inrange(age, 49.9, 59.1)
replace age_cat=5 if inrange(age, 59.9, 69.1)
replace age_cat=6 if inrange(age, 69.9, 120)

label define age_cat 1 "18-29" 2 "30-39" 3 "40-49" 4 "50-59" 5 "60-69" 6 "70+"
label values age_cat age_cat
codebook age_cat

*Education
codebook Q4
gen education=Q4 if timedummy==0
bysort common_id (timedummy) : replace education = education[_n-1] if missing(education)
codebook Q4 education if timedummy==0
recode education (4=2 "Vocational school") (1=1 "Elementary school") (2 3=3 "High school") (5=4 "Higher education, short") (6=5 "Higher education, middle") (7 8=6 "Higher education, long") , gen(education1)
drop education
rename education1 education
codebook education
tab timedummy education, row

*Occupation
codebook Q6, tab(99)
gen occupation=Q6 if timedummy==0
bysort common_id (timedummy) : replace occupation = occupation[_n-1] if missing(occupation)
codebook Q6 occupation if timedummy==0, tab(99)
recode occupation (9 10=6 "Other") (6=3 "Uemployed") (4 5=2 "Employee in public sector") (1 2 3=1 "Employee in private sector") (7=4 "Retired") (8=5 "Student"), gen(occupation1)
codebook occupation1 Q6 if timedummy==0, tab(99)
drop occupation
rename occupation1 occupation
tab timedummy occupation, row


*Region of residence
codebook Region
gen region=Region if timedummy==0
bysort common_id (timedummy) : replace region = region[_n-1] if missing(region)
codebook Region region if timedummy==0
recode region (1=1 "Copenhagen") (2=2 "Zealand") (3=3 "South Denmark") (4=4 "Mid-Jutland") (5=5 "North Jutland"), gen(region1)
codebook Region region1 if timedummy==0
drop region
rename region1 region
tab timedummy region, row

*Partisanship - vote choice in wave 1 (used for analysis where I restrict the pool to only include voters who support a party recommending a "Yes")
codebook Q20, tab(99)
gen partisanship=Q20 if timedummy==0
bysort common_id (timedummy) : replace partisanship = partisanship[_n-1] if missing(partisanship)
codebook Q20 partisanship if timedummy==0, tab(99)
recode partisanship (1 2 3 5 6 7 8 9 11 12 14=1 "Supports party recommending a YES") (4 10 13=2 "Supports party recommending a NO") (15 16 17 18 19 20=3 "Don't know/else"), gen(partisanship1)
codebook partisanship1 Q20 if timedummy==0, tab(99)
drop partisanship
rename partisanship1 partisanship
tab timedummy partisanship , row

*Using vote-choice in last election (measured in wave 1) as indicator of current partisanship in case of missing value
codebook Q19, tab(99)
gen priorpartisanship=Q19 if timedummy==0
bysort common_id (timedummy) : replace priorpartisanship = priorpartisanship[_n-1] if missing(priorpartisanship)
codebook Q19 priorpartisanship if timedummy==0, tab(99)
recode priorpartisanship (1 2 3 6 7 8 11 13=1 "Supports party recommending a YES") (4 9 12=2 "Supports party recommending a NO") (5 10 14 15 16 17 18 19=3 "Don't know/else"), gen(priorpartisanship1)
codebook priorpartisanship1 Q19 if timedummy==0, tab(99)
drop priorpartisanship
rename priorpartisanship1 priorpartisanship
tab timedummy priorpartisanship , row

replace partisanship=1 if partisanship==3 & priorpartisanship==1


*Prior EU values (used for matching)
codebook Q15
gen prior_EU_values=Q15 if timedummy==0
bysort common_id (timedummy) : replace prior_EU_values = prior_EU_values[_n-1] if missing(prior_EU_values)
codebook Q15 prior_EU_values if timedummy==0
recode prior_EU_values (1=5 "Clearly most benefits (of EU membership)") (2=4 "Predominantly most benefits") (3 6=3 "Neither nor (including don't knows)") (4=2 "Predominantly most disadvantages") (5=1 "Clearly most disadvantages (of EU membership)") , gen(prior_EU_values1)
codebook prior_EU_values1 Q15 if timedummy==0
drop prior_EU_values
rename prior_EU_values1 prior_EU_values
tab timedummy prior_EU_values, row

*Prior ideological self-placement (used for matching)
codebook Q22, tab(99)
gen prior_ideology=Q22 if timedummy==0
bysort common_id (timedummy) : replace prior_ideology = prior_ideology[_n-1] if missing(prior_ideology)
codebook Q22 prior_ideology if timedummy==0, tab(99)
replace prior_ideology=5 if prior_ideology==.
label define ideology 0 "Left" 10 "Right"
label values prior_ideology ideology
codebook prior_ideology, tab(99)
tab timedummy prior_ideology, row 



*********Time-varying control variables********


*Perceived government performance
codebook Q12
recode Q12 (1=5 "Very good") (2=4 "Good") (6 3=3 "Neither good nor bad") (4=2 "Bad") (5=1 "Very bad"), gen(perceived_governmentperformance)
codebook Q12 perceived_governmentperformance

*Interest in politics regarding the EU
codebook Q8
recode Q8 (1=5 "Very interested") (2=4 "Somewhat interested") (3=3 "Neither interested nor uninterested") (4=2 "Less interested") (5=1 "Not at all interested"), gen(EU_polinterest)
codebook Q8 EU_polinterest


*Knowledge of Danish defense Opt-Out (index construction)
codebook Q26 Q27 Q28
recode Q26 (1=1 "Correct answer") (2 3=0 "Don't know the right answer"), gen(knowledge1)
codebook Q26 knowledge1
recode Q27 (2=1 "Correct answer") (1 3=0 "Don't know the right answer"), gen(knowledge2)
codebook Q27 knowledge2
recode Q28 (1=1 "Correct answer") (2 3=0 "Don't know the right answer"), gen(knowledge3)
codebook Q28 knowledge3

pwcorr knowledge1 knowledge2 knowledge3
alpha knowledge1 knowledge2 knowledge3, item min(3)
alpha knowledge1 knowledge2 knowledge3, gen(knowledge_index) min(3)
hist knowledge_index
sum knowledge_index
codebook knowledge_index, tab(99)
recode knowledge_index (0=0 "0 answers corrects") (0.33333334=1 "1 answer correct") (0.66666669=2 "2 answers correct") (1=3 "3 answers correct"), gen(knowledge)
codebook knowledge_index knowledge, tab(99)
drop knowledge_index

*Issue attitude 1 - Abolishment will streghten Danish influence in EU-corporation
codebook Q29_2_resp
recode Q29_2_resp (1=5 "Completely agree (that abolishment will strenghten danish influence)") (2=4 "Somewhat agree") (3 6=3 "Neither agree nor disagree (including don't know)") (4=2 "Somewhat disagree") (5=1 "Completely disagree (that abolishment will strenghten danish influence)"), gen(issueattitude1)
codebook Q29_2_resp issueattitude1


*Issue attitude 2 - Abolishment is important to Danish security
codebook Q29_4_resp
recode Q29_4_resp (1=5 "Completely agree (that abolishment is important to Danish security)") (2=4 "Somewhat agree") (3 6=3 "Neither agree nor disagree (including don't know)") (4=2 "Somewhat disagree") (5=1 "Completely disagree (that abolishment is important to Danish security)"), gen(issueattitude2)
codebook Q29_4_resp issueattitude2


*Generating vote_change variable - for matching analysis (models 7 and 8)
gen vote_yes_change=.
sort common_id timedummy
by common_id (timedummy), sort: replace vote_yes_change=0 if vote_yes[_n-1]==0 & (vote_yes==0)
by common_id (timedummy), sort: replace vote_yes_change=0 if vote_yes[_n-1]==1 & (vote_yes==1)
by common_id (timedummy), sort: replace vote_yes_change=1 if vote_yes[_n-1]==0 & (vote_yes==1)
by common_id (timedummy), sort: replace vote_yes_change=-1 if vote_yes[_n-1]==1 & (vote_yes==0)
label define vote_yes_change 0 "Did not change vote" 1 "Changed to YES" -1 "Changed to NO"
label values vote_yes_change vote_yes_change
codebook vote_yes_change

*Adding values to misssing observation regarding independent variable (learning)
bysort common_id (timedummy) : replace vote_yes_change = vote_yes_change[_n+1] if missing(vote_yes_change)



**************ANALYSIS****************

*Dropping respondents who "forgot" policy info
drop if learn_policyinfo==3


***********Figure 1***************
reg vote_yes i.timedummy##i.learn_policyinfo, cl(common_id)
margins, over(timedummy learn_policyinfo)
marginsplot, scheme(plotplain) title("               2022 Danish Opt-Out Referendum", size(large)) subtitle({it:Policy Information: Reason for Supporting}, size(medium) ) xtitle("") ytitle("Proportion Voting Yes", size(medium)) legend(ring(0) size(small) position(0.0) order(6 "Always Knew" 5 "Learned Policy Information"  4 "Never Learned")) ylab(0(0.2)1) ylabel(0 "0%" 0.2 "20%" 0.4 "40%" 0.6 "60%" 0.8 "80%" 1 "100%", labsize(medsmall)) xscale(r(-0.2(1.4)1.2)) xlabel(0(1)1, valuelabels nogrid labsize(medsmall)) xsize(4.5) xline(2.8) ciopts(recast(rspike)) plot1opts(mfcolor(gray)lpattern("1")msym(O)msize(medium)) plot2opts(mfcolor(black)lpattern("1")msym(O)msize(medium)) plot3opts(mfcolor(white)lpattern("1")msym(O)msize(medium)) xoverhangs text(0.1847619   -0.10 "18%" 0.4735376   -0.1 "47%" 0.7195946   -0.1 "72%" 0.2342857  1.1 "23%" 0.6824513  1.1 "68%" 0.8141892   1.1 "81%", size(medsmall))


*Dropping respondents who "always knew" policy info
drop if learn_policyinfo==2


***All voters****

*Model 1
reg vote_yes i.timedummy##i.learn_policyinfo, cl(common_id)

*Model 2
reg vote_yes i.timedummy##i.learn_policyinfo perceived_governmentperformance EU_polinterest knowledge issueattitude1 issueattitude2, cl(common_id)

*Model 3
reg vote_yes i.timedummy##(i.learn_policyinfo i.gender i.age_cat i.education i.region i.occupation) perceived_governmentperformance EU_polinterest knowledge issueattitude1 issueattitude2, cl(common_id)


****Voters whose party recommend voting yes (in favour of the referendum proposal)****

*Model 4
reg vote_yes i.timedummy##i.learn_policyinfo if partisanship==1, cl(common_id)

*Model 5
reg vote_yes i.timedummy##i.learn_policyinfo perceived_governmentperformance EU_polinterest knowledge issueattitude1 issueattitude2 if partisanship==1, cl(common_id)

*Model 6
reg vote_yes i.timedummy##(i.learn_policyinfo i.gender i.age_cat i.education i.region i.occupation) perceived_governmentperformance EU_polinterest knowledge issueattitude1 issueattitude2 if partisanship==1, cl(common_id)

****Matched Group****
psmatch2 learn_policyinfo i.gender i.age_cat i.education i.region i.occupation prior_ideology prior_EU_values if timedummy==0, out( vote_yes_change ) n(1) noreplacement
bysort common_id (timedummy) : replace _weight = _weight[_n-1] if missing(_weight)

*Model 7
reg vote_yes i.timedummy##i.learn_policyinfo if _weight==1, cl(common_id)

*Model 8
reg vote_yes i.timedummy##i.learn_policyinfo perceived_governmentperformance EU_polinterest knowledge issueattitude1 issueattitude2 if _weight==1, cl(common_id)

